<?php
include_once ('db.class.php');

class thunder extends mydb {
	
	// 构造
	function __construct() {
		parent::__construct ();
	
	}
	
	// 析构
	function __destruct() {
		//$this->close ();
	}
	
	function createNewYHDA($data) {
		$yhid = $this->getNextCode ( YHDA_TABLE );
		$sql = "INSERT INTO `" . YHDA_TABLE . "` (yhid, lxr,pym,yhlx,yhdw,yhdz,yzbm,yhdh,yhsj, jcyj, jcyq, bgyxq, bgbh, gjrq, jdr, bz)values('" . $yhid . "','" . $data ['lxr'] . "','" . $data ['pym'] . "','" . $data ['yhlx'] . "','" . $data ['yhdw'] . "','" . $data ['yhdz'] . "','" . $data ['yzbm'] . "','" . $data ['yhdh'] . "','" . $data ['yhsj'] . "', '" . $data ['jcyj'] . "', '" . $data ['jcyq'] . "', '" . $data ['bgyxq'] . "', '" . $data ['bgbh'] . "', '" . $data ['gjrq'] . "', '" . $data ['jdr'] . "', '" . $data ['bz'] . "')";
		$result = $this->query ( $sql );
	}
	
	function createNewJCJL($data) {
		$sql = "INSERT INTO `" . JCJL_TABLE . "` 
		          (yhid, building, bheight, bthunder, flush_shape, flush_height, flush_texture, flush_spec, flush_connect, flush_rust, jd_texture, jd_spec,  jd_connect, jd_rust, kj_texture, kj_spec, kj_connect, dz_jdreal, dz_jdend, dz_kjreal, dz_kjend, thunder, jcrq )values
		          ('".$data['yhid']."', 
		          '".$data['building']."', 
		          '".$data['bheight']."', 
		          '".$data['bthunder']."', 
		          '".$data['flush_shape']."', 
		          '".$data['flush_height']."', 
		          '".$data['flush_texture']."', 
		          '".$data['flush_spec']."', 
		          '".$data['flush_connect']."', 
		          '".$data['flush_rust']."', 
		          '".$data['jd_texture']."', 
		          '".$data['jd_spec']."',  
		          '".$data['jd_connect']."', 
		          '".$data['jd_rust']."', 
		          '".$data['kj_texture']."', 
		          '".$data['kj_spec']."', 
		          '".$data['kj_connect']."', 
		          '".$data['dz_jdreal']."', 
		          '".$data['dz_jdend']."', 
		          '".$data['dz_kjreal']."', 
		          '".$data['dz_kjend']."', 
		          '".$data['thunder']."',
		          '".$data['jcrq']."'
		          )";
		$result = $this->query ( $sql );
	}
	
	function createNewJCGS($data) {
		$sql = "INSERT INTO `" . JCGS_TABLE . "` 
		      (jcqk, memo, weather, shape, jcr, yhid, jcrq)values
		      ('".$data['jcqk']."', 
		      '".$data['memo']."', 
		      '".$data['weather']."', 
		      '".$data['shape']."', 
		      '".$data['jcr']."', 
		      '".$data['yhid']."',
		      '".$data['jcrq']."'
		      )";
		$result = $this->query ( $sql );
	}
	
	function createNewArticle($aid, $article) {
		$sql = "INSERT INTO `" . ARTICLES_TABLE . "` (bid, article)values('" . $aid . "', '" . $article . "')";
		$result = $this->query ( $sql );
	}
	
	function editYHDA($data) {
		$sql = "UPDATE `" . YHDA_TABLE . "` SET 
		      lxr = '" . $data ['lxr'] . "',
		      pym = '" . $data ['pym'] . "',
		      yhlx = '" . $data ['yhlx'] . "',
		      yhdw = '" . $data ['yhdw'] . "',
		      yhdz = '" . $data ['yhdz'] . "',
		      yzbm = '" . $data ['yzbm'] . "',
		      yhdh = '" . $data ['yhdh'] . "',
		      yhsj = '" . $data ['yhsj'] . "', 
		      jcyj = '" . $data ['jcyj'] . "', 
		      jcyq = '" . $data ['jcyq'] . "', 
		      bgyxq = '" . $data ['bgyxq'] . "', 
		      bgbh = '" . $data ['bgbh'] . "', 
		      gjrq = '" . $data ['gjrq'] . "', 
		      bz = '" . $data ['bz'] . "'
		      WHERE id = ".$data['id'];
		$result = $this->query ( $sql );
	}
	
	function editJCJL($data) {
		$sql = "UPDATE `" . JCJL_TABLE . "` SET 
		          building = '".$data['building']."', 
                  bheight = '".$data['bheight']."', 
                  bthunder = '".$data['bthunder']."', 
                  flush_shape = '".$data['flush_shape']."', 
                  flush_height = '".$data['flush_height']."', 
                  flush_texture = '".$data['flush_texture']."', 
                  flush_spec = '".$data['flush_spec']."', 
                  flush_connect = '".$data['flush_connect']."', 
                  flush_rust = '".$data['flush_rust']."', 
                  jd_texture = '".$data['jd_texture']."', 
                  jd_spec = '".$data['jd_spec']."',  
                  jd_connect = '".$data['jd_connect']."', 
                  jd_rust = '".$data['jd_rust']."', 
                  kj_texture = '".$data['kj_texture']."', 
                  kj_spec = '".$data['kj_spec']."', 
                  kj_connect = '".$data['kj_connect']."', 
                  dz_jdreal = '".$data['dz_jdreal']."', 
                  dz_jdend = '".$data['dz_jdend']."', 
                  dz_kjreal = '".$data['dz_kjreal']."', 
                  dz_kjend = '".$data['dz_kjend']."', 
                  thunder = '".$data['thunder']."',
                  jcrq = '".$data['jcrq']."'
                  WHERE id=".$data['jlid'];
		$result = $this->query ( $sql );
	}
	
	function editJCGS($data) {
		$sql = "UPDATE `" . JCGS_TABLE . "` SET 
		          jcqk = '".$data['jcqk']."', 
                  memo = '".$data['memo']."', 
                  weather = '".$data['weather']."', 
                  shape = '".$data['shape']."',
                  jcrq = '".$data['jcrq']."'
                  WHERE id =".$data['id'];
		$result = $this->query ( $sql );
	}
	
	function editArticle($aid, $article) {
		$sql = "UPDATE `" . ARTICLES_TABLE . "` SET article='" . $article . "' WHERE bid='" . $aid . "'";
		$result = $this->query ( $sql );
	}
	
	/*
    * 取用户档案
    * @param int $id 
    * @return one record
    * */
	function getYHDA($id) {
		$data = array ();
		$sql = "SELECT * FROM " . YHDA_TABLE . " WHERE id = '" . $id . "'";
		$result = $this->query ( $sql );
		if ($row = $this->fetchArray ( $result )) {
			$data = $row;
		}
		
		return $data;
	}
	
	/*
    * 取检测记录列表
    * @param int $yhid 用户ID
    * 
    * @return list 检测记录列表
    * */
	function getJCJLs($yhid) {
		$data = array ();
		$sql = "SELECT  * FROM `" . JCJL_TABLE . "` WHERE yhid='" . $yhid . "' ORDER BY id DESC";
		$result = $this->query ( $sql );
		while($row = $this->fetchArray ( $result )) {
			$data[] = $row;
		}
		
		return $data;
	}
	
/*
    * 取检测记录列表
    * @param int $yhid 用户ID
    * @param date $rq
    * @param date $yxq 
    * @return list 检测记录列表
    * */
    function getJCJLsd($yhid, $jcrq, $yxq) {
        $data = array ();
        $sql = "SELECT  * FROM `" . JCJL_TABLE . "` WHERE yhid='" . $yhid . "' AND jcrq >='".$jcrq."' AND jcrq<='".$yxq."' ORDER BY id DESC";
        $result = $this->query ( $sql );
        while($row = $this->fetchArray ( $result )) {
            $data[] = $row;
        }
        
        return $data;
    }
	
	/*
    * 取检测记录
    * @param int $jlid 记录ID
    * 
    * @return array 记录信息
    * */
	function getJCJL($jlid) {
		$data = array ();
		$sql = "SELECT * FROM `" . JCJL_TABLE . "` WHERE id='" . $jlid . "'";
		$result = $this->query ( $sql );
		if ($row = $this->fetchArray ( $result )) {
			$data = $row;
		}
		
		return $data;
	}
	
	// 取检测概述列表
	function getJCGSs($yhid) {
		$data = array ();
		$sql = "SELECT * FROM " . JCGS_TABLE . " WHERE  yhid = '" . $yhid . "' ORDER BY id DESC";
		$result = $this->query ( $sql );
		while ( $row = $this->fetchArray ( $result ) ) {
			$data [] = $row;
		}
		return $data;
	}
	
	/*
	 * 取检测概述信息
	 * @param int $gsid 书目代码
	 * 
	 * 
	 * @return array
	 */
	function getJCGS($gsid) {
		$data = array ();
		$sql = "SELECT * FROM " . JCGS_TABLE . " WHERE id=" . $gsid;
		$result = $this->query ( $sql );
		if ( $row = $this->fetchArray ( $result ) ) {
			$data = $row;
		}
		
		return $data;
	}
	
	/*
     * 取小节列表
     * @param String $cid 小节ID
     * 
     * 
     * @return list
     */
	function getLastJCGS($yhid) {
		$data = array ();
		$sql = "SELECT * FROM " . JCGS_TABLE . " WHERE  yhid = '" . $yhid . "' ORDER BY id DESC LIMIT 1";
		$result = $this->query ( $sql );
		if ( $row = $this->fetchArray ( $result ) ) {
			$data = $row;
		}
		return $data;
	}
	
	/*
     * 取小节列表
     * @param String $lid 小节ID
     * 
     * 
     * @return list
     */
	function getCListFromLID($lid) {
		$data = array ();
		$sql = "SELECT a.bid as bid, a.bcode as bookcode, a.bname as bookname, b.bid as cid, b.bcode as contentcode, b.bname as contentname, c.bid as lid, c.bname as listname 
                FROM " . BOOKS_TABLE . " a
                LEFT JOIN " . BOOKS_TABLE . " b ON a.bid = b.parent
                LEFT JOIN " . BOOKS_TABLE . " c ON b.bid = c.parent
                WHERE c.bid='" . $lid . "' ";
		$result = $this->query ( $sql );
		while ( $row = $this->fetchArray ( $result ) ) {
			$data [] = $row;
		}
		return $data;
	}
	
	/*
     * 取文章
     * @param int $art 文章ID
     * @return array
     */
	function getArticle($art) {
		$data = array ();
		$sql = "SELECT * 
                FROM " . ARTICLES_TABLE . " a
                WHERE bid='" . $art . "' ";
		$result = $this->query ( $sql );
		while ( $row = $this->fetchArray ( $result ) ) {
			$data [] = $row;
		}
		return $data;
	}
	
	/* 判断书目中的章节是否存在
	 * 
    * @param String $bookcode 书目代码
    * @param String $contentcode 章节代码
    * @return true or false
    * */
	function isExistContent($bookcode, $contentcode) {
		$data = array ();
		$re = FALSE;
		$sql = "SELECT a.bid as bid, a.bcode as bookcode, a.bname as bookname, b.bid as cid, b.bcode as contentcode, b.bname as contentname
		      FROM `" . BOOKS_TABLE . "` a
		      LEFT JOIN `" . BOOKS_TABLE . "` b ON a.bid = b.parent 
		      WHERE a.bcode='" . $bookcode . "' AND b.bcode='" . $contentcode . "'";
		$result = $this->query ( $sql );
		while ( $row = $this->fetchArray ( $result ) ) {
			$data [] = $row;
		}
		if (count ( $data ) > 0) {
			$re = TRUE;
		}
		echo var_dump ( $data );
		return $re;
	}
	
	/** 取下一个编号
	 * @return formated number
	 * 
	 **/
	public function getNextCode($table) {
		global $db;
		$row = $this->getOne ( 'SELECT COUNT(*) AS num FROM `' . $table . '` WHERE 1' );
		$num = $row ['num'] ? $row ['num'] : '0';
		$tmp = sprintf ( "%05d", $num );
		return 'YH' . $tmp;
	}
}
?>